﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using System.Threading;

namespace NotMyBoom
{

    public class MyTextbox : MyWindow
    {
        int maxLenght;
        public MyTextbox(Vector2 TopLeft, int Width, int Height, int MaxLenght) 
        {
            this.TopLeft = new Vector2(TopLeft.X,TopLeft.Y);
            this.Width = Width;
            this.Height = Height;
            this.Color = Color.Blue;
            this.Text = "";
            Size = new Vector2(Width,Height);
            selected = false;
            count = 0;
            maxLenght = MaxLenght;
        }

        public override void Init(ContentManager content)
        {
            font = content.Load<SpriteFont>(@"font/font1");
            base.Init(content);
        }

        bool selected;
        int count = 0;
        bool flag = true;

        string temp = "";

        public bool Selected
        {
            get { return selected; }
            set { selected = value; }
        }

        public override void Update(GameTime gameTime)
        {
            
                KeyboardState keyState = Keyboard.GetState();

                MouseState mouseState = Mouse.GetState();
                if (mouseState.LeftButton == ButtonState.Pressed)
                    if (IsClicked(new Vector2(mouseState.X, mouseState.Y)))
                        selected = true;
                    else
                        selected = false;
                text = temp;
                if (selected)
                {
                    if (count % 9 == 0)
                        flag = true;
                    if (count - 10 < 0)
                        text += "|";

                }
                else flag = false;
                if (flag)
                {
                    if (keyState.IsKeyDown(Keys.Back))
                        if (temp.Length > 0)
                            temp = temp.Remove(temp.Length - 1, 1);
                    if (text.Length < maxLenght)
                        temp += GetKey();
                    flag = false;
                }
                count = (count + 1) % 20;               
            
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
           // if (BackGroundImage != null)
           //     spriteBatch.Draw(BackGroundImage, TopLeft, Color.White);
           // else 
                spriteBatch.Draw(CreateRect(spriteBatch.GraphicsDevice,this.Width,this.Height,Color.White), TopLeft, Color.White);
            spriteBatch.DrawString(font, text, new Vector2(TopLeft.X + 5, TopLeft.Y + 4),
                color, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0.5f);
        }

        string GetKey()
        {            
            KeyboardState keyState = Keyboard.GetState();
            if (keyState.IsKeyDown(Keys.LeftShift))
            {
                if (keyState.IsKeyDown(Keys.A))
                    return "A";
                if (keyState.IsKeyDown(Keys.B))
                    return "B";
                if (keyState.IsKeyDown(Keys.C))
                    return "C";
                if (keyState.IsKeyDown(Keys.D))
                    return "D";
                if (keyState.IsKeyDown(Keys.E))
                    return "E";
                if (keyState.IsKeyDown(Keys.F))
                    return "F";
                if (keyState.IsKeyDown(Keys.G))
                    return "G";
                if (keyState.IsKeyDown(Keys.H))
                    return "H";
                if (keyState.IsKeyDown(Keys.I))
                    return "I";
                if (keyState.IsKeyDown(Keys.J))
                    return "J";
                if (keyState.IsKeyDown(Keys.K))
                    return "K";
                if (keyState.IsKeyDown(Keys.L))
                    return "L";
                if (keyState.IsKeyDown(Keys.M))
                    return "M";
                if (keyState.IsKeyDown(Keys.N))
                    return "N";
                if (keyState.IsKeyDown(Keys.O))
                    return "O";
                if (keyState.IsKeyDown(Keys.P))
                    return "P";
                if (keyState.IsKeyDown(Keys.Q))
                    return "Q";
                if (keyState.IsKeyDown(Keys.R))
                    return "R";
                if (keyState.IsKeyDown(Keys.S))
                    return "S";
                if (keyState.IsKeyDown(Keys.T))
                    return "T";
                if (keyState.IsKeyDown(Keys.Y))
                    return "Y";
                if (keyState.IsKeyDown(Keys.U))
                    return "U";
                if (keyState.IsKeyDown(Keys.Z))
                    return "Z";
                if (keyState.IsKeyDown(Keys.X))
                    return "X";
                if (keyState.IsKeyDown(Keys.V))
                    return "V";
                if (keyState.IsKeyDown(Keys.W))
                    return "W";
            }
            else
            {
                if (keyState.IsKeyDown(Keys.A))
                    return "a";
                if (keyState.IsKeyDown(Keys.B))
                    return "b";
                if (keyState.IsKeyDown(Keys.C))
                    return "c";
                if (keyState.IsKeyDown(Keys.D))
                    return "d";
                if (keyState.IsKeyDown(Keys.E))
                    return "e";
                if (keyState.IsKeyDown(Keys.F))
                    return "f";
                if (keyState.IsKeyDown(Keys.G))
                    return "g";
                if (keyState.IsKeyDown(Keys.H))
                    return "h";
                if (keyState.IsKeyDown(Keys.I))
                    return "i";
                if (keyState.IsKeyDown(Keys.J))
                    return "j";
                if (keyState.IsKeyDown(Keys.K))
                    return "k";
                if (keyState.IsKeyDown(Keys.L))
                    return "l";
                if (keyState.IsKeyDown(Keys.M))
                    return "m";
                if (keyState.IsKeyDown(Keys.N))
                    return "n";
                if (keyState.IsKeyDown(Keys.O))
                    return "o";
                if (keyState.IsKeyDown(Keys.P))
                    return "p";
                if (keyState.IsKeyDown(Keys.Q))
                    return "q";
                if (keyState.IsKeyDown(Keys.R))
                    return "r";
                if (keyState.IsKeyDown(Keys.S))
                    return "s";
                if (keyState.IsKeyDown(Keys.T))
                    return "t";
                if (keyState.IsKeyDown(Keys.Y))
                    return "y";
                if (keyState.IsKeyDown(Keys.U))
                    return "u";
                if (keyState.IsKeyDown(Keys.Z))
                    return "z";
                if (keyState.IsKeyDown(Keys.X))
                    return "x";
                if (keyState.IsKeyDown(Keys.V))
                    return "v";
                if (keyState.IsKeyDown(Keys.W))
                    return "w";
            }
            if (keyState.IsKeyDown(Keys.NumPad0))
                return "0";
            if (keyState.IsKeyDown(Keys.NumPad1))
                return "1";
            if (keyState.IsKeyDown(Keys.NumPad2))
                return "2";
            if (keyState.IsKeyDown(Keys.NumPad3))
                return "3";
            if (keyState.IsKeyDown(Keys.NumPad4))
                return "4";
            if (keyState.IsKeyDown(Keys.NumPad5))
                return "5";
            if (keyState.IsKeyDown(Keys.NumPad6))
                return "6";
            if (keyState.IsKeyDown(Keys.NumPad7))
                return "7";
            if (keyState.IsKeyDown(Keys.NumPad8))
                return "8";
            if (keyState.IsKeyDown(Keys.NumPad9))
                return "9";
            return "";
        }

    }
}
